/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: messageview.h 388 2009-05-06 18:52:23Z jpnurmi $
*/

#ifndef MESSAGEVIEW_H
#define MESSAGEVIEW_H

#include "ui_messageview.h"

class NickHighlighter;
class QAbstractItemModel;

class MessageView : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QString receiver READ receiver WRITE setReceiver)
    Q_PROPERTY(int zoomLevel READ zoomLevel WRITE setZoomLevel)

public:
    MessageView(const QString& receiver, QWidget* parent = 0);
    ~MessageView();

    QString receiver() const;
    void setReceiver(const QString& receiver);

    uint getId() const;
    void setId(uint id);

    bool matches(const QString& receiver) const;
    bool contains(const QString& sender) const;

    QAbstractItemModel* model() const;
    void setModel(QAbstractItemModel* model);

    void receiveHelp(const QString& message);
    void receiveMessage(const QString& sender, const QString& message, bool highlight = false);
    void receiveNotice(const QString& sender, const QString& message, bool highlight = false);
    void receiveAction(const QString& sender, const QString& message, bool highlight = false);
    void logMessage(const QString& sender, const QString& format, const QString& message, bool highlight = false);
    void setTopic(const QString& topic);

    int zoomLevel() const;
    void setZoomLevel(int level);

public slots:
    void zoomIn();
    void zoomOut();
    void clear();
    void find();
    void findNext();
    void findPrevious();

protected:
    bool eventFilter(QObject* receiver, QEvent* event);
    void hideEvent(QHideEvent* event);

signals:
    void send(const QString& text);
    void query(const QString& nick);
    void command(const QString& command);

private slots:
    void onSend();
    void onTabPressed();
    void onTextEdited();
    void insertCompletion(const QString& completion);
    void moveToNextPage();
    void moveToPrevPage();
    void find(const QString& text, bool forward = false, bool backward = false);
    void onAnchorClicked(const QUrl& link);
    void onActionTriggered();

private:
    QString prefixedSender(const QString& sender) const;
    QString processSender(const QString& sender) const;
    QString processMessage(const QString& message) const;

    struct MessageViewData : public Ui::MessageView
    {
        mutable QString prefixCache;
        QString receiver;
		uint id;
        QCompleter* completer;
        NickHighlighter* highlighter;
    } d;
};

#endif // MESSAGEVIEW_H
